package com.food.user.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
public class RedisUtil {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    //得到键值对
    public String getValue(String key){
        return stringRedisTemplate.opsForValue().get(key);
    }
    //创建一个键值对并设置有效时间
    public void setValue(String key,String value,Long seconds){
        stringRedisTemplate.opsForValue().set(key,value,seconds, TimeUnit.SECONDS);
    }
    //创建序列化的键值对
    public void setValue(String key,String value){
        stringRedisTemplate.opsForValue().set(key,value);
    }

    //删除数据
    public void delete(String key){
        stringRedisTemplate.delete(key);
    }
    //判断key是否存在
    public boolean hasKey(String key){
       return stringRedisTemplate.hasKey(key);
    }

    //自增key string类型
    public void inc(String key){
        stringRedisTemplate.opsForValue().increment(key);
    }

    //set 新增
    public void sAdd(String key,String value){
        stringRedisTemplate.opsForSet().add(key,value);
    }

    //set 中判断是否存在某个值
    public boolean sIsMember(String key,String value){
       return stringRedisTemplate.opsForSet().isMember(key,value);
    }

    //从set中删除某值
    public void sRem (String key,String value){
        stringRedisTemplate.opsForSet().remove(key,value);
    }

    //查询set中全部值
    public Set sMembers(String key){
       return stringRedisTemplate.opsForSet().members(key);
    }


    //zet增加
    public void zAdd(String key,String value,double num){
        stringRedisTemplate.opsForZSet().add(key,value,num);
    }


    //zSet 增加某值排序值
    public void zIncrBy(String key,String value,double num){
        stringRedisTemplate.opsForZSet().incrementScore(key,value,num);
    }

   public double zScore(String key,String value){
       return stringRedisTemplate.opsForZSet().score(key,value);
   }

    public double  zRem(String key,String value){
        return stringRedisTemplate.opsForZSet().remove(key,value);
    }

    public Set  zRevRange(String key,Long start,Long end){
      return stringRedisTemplate.opsForZSet().reverseRange(key,start,end);
    }

    public void hIncrBy(String key,String hashKey,double number){
        stringRedisTemplate.opsForHash().increment(key,hashKey,number);
    }

    public void putAll(String key,Map<String,Object> map){
        stringRedisTemplate.opsForHash().putAll(key, map);
    }

    public void put(String key,String hashKey,String value){
        stringRedisTemplate.opsForHash().put(key,hashKey,value);
    }

    public Object hGet(String key,String hashKey){
        return  stringRedisTemplate.opsForHash().get(key,hashKey);
    }

    public void hrem(String key,String hashKey){
        stringRedisTemplate.opsForHash().delete(key,hashKey);
    }

    public Map<Object, Object> entries(String key){
        return  stringRedisTemplate.opsForHash().entries(key);
    }
}
